import {
  Request,
  Controller,
  Post,
  UseGuards,
  Body,
  Get,
} from '@nestjs/common';
import {
  ApiBasicAuth,
  ApiBearerAuth,
  ApiBody,
  ApiHeader,
  ApiHeaders,
  ApiTags,
} from '@nestjs/swagger';
import { ResponseData } from '../../common/interfaces/result.interface';
import { LoginUserDto } from './dto/login-user.dto';
import { JwtAuthGuard } from './guards/jwt-auth.guard';
import { LocalAuthGuard } from './guards/local-auth.guard';

import { UserService } from './user.service';

@ApiTags('登录注册')
@Controller('public')
export class PublicController {
  constructor(private readonly userService: UserService) {}
  // @UseGuards(LocalAuthGuard)
  // @Post('login')
  // // @ApiBody({ type: [LoginUserDto] })
  // async login(@Request() req): Promise<ResponseData> {
  //   console.log('controller user', req.user)
  //   return this.userService.login(req.user)
  // }

  @UseGuards(JwtAuthGuard)
  // @ApiBearerAuth()
  @ApiBasicAuth()
  @Get('test')
  async test(@Request() req) {
    console.log('user:::', req.user);
    return 'ok';
    // const { iat, exp, ...result } = req.user;
    // console.log('eee', exp - iat);
    // console.log('user', req.user);
    //curl -X GET http://localhost:3001/user/my -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsInVzZXJuYW1lIjoiYWRtaW4iLCJpYXQiOjE2MTA5Mjk1NjcsImV4cCI6MTYxMDkzMzE2N30.SXMhdidnUlJCoxs78eYTd4zSU7iZiNj_3un1B-Z7Gh4"
    // return result;
  }

  @UseGuards(LocalAuthGuard)
  @Post('login')
  async login(
    @Body() userDto: LoginUserDto,
    @Request() req,
  ): Promise<ResponseData> {
    return this.userService.login(req.user);
  }
}
